<?php

namespace app\common\library;

use think\Env;
use think\Config;
use think\View;

use app\admin\model\ImsVlan;
use app\admin\model\ImsAccount;

class IMSHelper
{

    protected $fsTplPath = NULL;
    protected $fsConfPath = NULL;

    protected $view = NULL;

    protected $fsHelper = NULL;

    /**
     * 构造函数
     * NULL
     */
    public function __construct() {
        $this->fsTplPath = Env::get('fs.tpl.path', '');
        $this->fsConfPath = Env::get('fs.conf.path', '');
        //$this->fsHelper = new FSHelper();
    }

    public function __destruct() {
    }

    protected function my_exec($cmd, $input='') 
         {$proc=proc_open($cmd, array(0=>array('pipe', 'r'), 1=>array('pipe', 'w'), 2=>array('pipe', 'w')), $pipes); 
          fwrite($pipes[0], $input);fclose($pipes[0]); 
          $stdout=stream_get_contents($pipes[1]);fclose($pipes[1]); 
          $stderr=stream_get_contents($pipes[2]);fclose($pipes[2]); 
          $rtn=proc_close($proc); 
          return array('stdout'=>$stdout, 
                       'stderr'=>$stderr, 
                       'return'=>$rtn 
         ); 
    }

    public function addVlan($vlanid, $params) {
//echo "addVlan: {$vlanid}\r\n";
        $params = json_decode(json_encode($params), true);
        $params['vlanid'] = $vlanid;
        //var_dump($params);die($vlanid);
        $cmd = "sudo ip link add link ens37 name ens37.{$params['vlanid']} type vlan id {$params['vlanid']}";
        $cmd = $cmd . " && sudo ifconfig ens37.{$params['vlanid']} up";
        //$res = system($cmd);
        $cmd = $cmd . " && sudo ip link add link ens37.{$params['vlanid']} dev vth{$params['vlanid']} type macvlan";
        $cmd = $cmd . " && sudo ifconfig vth{$params['vlanid']} hw ether {$params['mac']}";
        $cmd = $cmd . " && sudo ifconfig vth{$params['vlanid']} {$params['ip']} netmask {$params['mask']}";
        $cmd = $cmd . " && sudo ifconfig vth{$params['vlanid']} up";
//die($cmd);
        $this->my_exec($cmd);
        $cmd = "echo \"{$params['vlanid']} vlan{$params['vlanid']}\" >> \/etc\/iproute2\/rt_tables";
        $cmd = $cmd . " && sudo ip route add default dev vth{$params['vlanid']} via {$params['gateway']} src {$params['ip']} table vlan{$params['vlanid']}";
        $cmd = $cmd . " && sudo ip rule add from {$params['ip']}\/32 table vlan{$params['vlanid']}";
        $cmd = $cmd . " && sudo ip route flush cache";
        $res = $this->my_exec($cmd);
        //var_dump($res);
        //var_dump($res);die();
        //$this->fsHelper->addIMSExternalProfile($params);
    }

    public function delVlan($vlanid, $params) {
//echo "delVlan: {$vlanid}\r\n";
        $params = json_decode(json_encode($params), true);
        $params['vlanid'] = $vlanid;
        $cmd = "sudo ip rule del lookup vlan{$params['vlanid']}";
        $cmd = $cmd . " && sudo ip route delete default dev vth{$params['vlanid']} table vlan{$params['vlanid']}";
        $cmd = $cmd . " && sudo ip route flush cache";
        $cmd = $cmd . " && sed -i \/vlan{$params['vlanid']}\/d \/etc\/iproute2\/rt_tables";
        $res = $this->my_exec($cmd);
//var_dump($res);
        //var_dump($params);die($vlanid);
        $cmd = "sudo ip link del link dev vth{$params['vlanid']}";
        //system($cmd);
        $cmd = $cmd . " & sudo ip link del link dev ens37.{$params['vlanid']}";
        $res = $this->my_exec($cmd);
        //var_dump($res);die('');
        //$this->fsHelper->delIMSExternalProfile($params);
    }

}
